🏃 AtCoder 環境構築 for Python
AtCoder 環境構築 for Python 使い向け
unyacat.icon うにゃです.
この記事を読んでできること
いい感じにフォルダ/ファイルを生成/整理してくれる.
AtCoder のテストケースを自動でチェックしてくれるようになる
導入するもの
online-judge-tools
atcoder-cli
環境
Windows 10 Pro
Python 3.7.7
今やるなら Python のバージョンは AtCoder のバージョンに合わせるべき
pip 20.1
npm 6.13.4
早速やっていこう.
インストール
pip は Python の,npm は JavaScript パッケージマネージャー.
npm と無縁の人間は 🔥 Firebase はいいぞ ~導入編~ を参考にすると幸せになれるかも.
code:command
pip install online-judge-tools
npm install -g atcoder-cli
Macで書き込み権限がないって怒られる場合 sudo -H pip3 insttall online-judge-tools としてみる
ログイン
提出が効かないことがあるっぽいが,一応ログインする.
ますは online-judge-tools のほう.
code:command
oj login https://atcoder.jp/
x service recognized: AtCoderService.from_url('https://atcoder.jp/'): https://atcoder.jp/
x GET: https://atcoder.jp/contests/agc001/submit
x 302 Found
! You are not signed in.
ERROR Selenium is not installed: try $ pip3 install selenium
! use CUI login since Selenium fails
x GET: https://atcoder.jp/contests/agc001/submit
x 302 Found
x GET: https://atcoder.jp/login
x 200 OK
Username: (Your AtCoder Username)
Password: (ここ見えないけど打てている)
x POST: https://atcoder.jp/login
x redirected: https://atcoder.jp/home
x 200 OK
! AtCoder says: × Welcome, (Your AtCoder Username).
+ Welcome,
x GET: https://atcoder.jp/contests/agc001/submit
x 200 OK
* You have already signed in.
x save cookie to: C:\Users\(username)\AppData\Local\online-judge-tools\online-judge-tools\cookie.jar
atcoder-cli も同様に
code:command
acc login
(略)
Config を正しく書く.
Config ファイルの場所は以下のように調べる.
code:command
acc config-dir
C:\Users\(username)\AppData\Roaming\atcoder-cli-nodejs\Config
まず,config.json
code:config.json
{
"oj-path": "C:\\Users\\(username)\\PycharmProjects\\atcoder\\venv\\Scripts\\oj.exe",
"default-contest-dirname-format": "{ContestID}",
"default-task-dirname-format": "{tasklabel}",
"default-test-dirname-format": "test",
"default-task-choice": "inquire",
"default-template": "python"
}
主な変更点は,
default-test-dirname-format: "test"
online-judge-tools のほうは "test" ディレクトリを探すので予めテストケースを格納するディレクトリは "test" にするようにする.
default-template: "python"
次に python ディレクトリをつくる.
(ここまで何度か出ていますがディレクトリはフォルダのことです...)
中身は main.py と template.json を作る.
code:main.py
(中身は空でも大丈夫)
code:template.json
{
"task": {
"program": [
"main.py"
],
"submit": "main.py"
}
}
これをしておくことでテストケースを保存したときに main.py も生成されるようになります.
結果として以下のようになっているといい感じ.
code:command
C:\Users\(username)\AppData\Roaming\atcoder-cli-nodejs\Config> tree /a /f
C:.
| config.json
| session.json
|
\---python
main.py
template.json
導入完了.
使う
AtCoder Beginners Contest 160 をやりたいとしましょう.abc160
code:command
acc new abc160
とすると,どの問題を保存するかを聞かれ (a -> Enter で全選択),以下のようなディレクトリが自動的に生成される.
https://gyazo.com/4f18889ab9d136a87b79119dc8b75058
今回は A 問題を解きます.
さっそくやり始めます(ゴニョゴニョ...
できた!
code:main.py
s = input()
if s3 == s4 and s5 == s6:
print("Yes")
else:
print("No")
それでは早速テストしてみましょう.テストのコマンドは,
code:command
oj t -c "python main.py"
です.
結果は...
code:command
oj t -c "python main.py"
* 3 cases found
! GNU time is not available: time
* sample-1
x time: 0.028419 sec
- WA
output:
No\r
expected:
Yes
* sample-2
x time: 0.027554 sec
! WA if no rstrip
+ AC
* sample-3
x time: 0.027355 sec
- WA
output:
No\r
expected:
Yes
x slowest: 0.028419 sec (for sample-1)
- test failed: 1 AC / 3 cases
間違っているようです.(ガチでやらかした)
ああそういえば文字列の1文字目は s[0] でした...
code:python
s = input()
if s2 == s3 and s4 == s5:
print("Yes")
else:
print("No")
良さげですね.
それではもう一回チャレンジ.
code:command
oj t -c "python main.py"
* 3 cases found
! GNU time is not available: time
* sample-1
x time: 0.028183 sec
! WA if no rstrip
+ AC
* sample-2
x time: 0.028151 sec
! WA if no rstrip
+ AC
* sample-3
x time: 0.027111 sec
! WA if no rstrip
+ AC
x slowest: 0.028183 sec (for sample-1)
+ test success: 3 cases
テストケースが通りました!これ安心して提出できますね!!
このような初歩的なミスを防いでくれるので環境構築は大切です.
慢心により提出していれば 5 分のペナルティを食らっていましたね.
このように環境構築をする時間に比べれば大会中にストレスがかからない利点が大きく大変オトクだと思います.
みなさんも是非.
twoooooda.icon大変参考になりました
unyacat.iconうれしい
#記事
#Atcoder
#unyacat